package com.yimi.yinhepda.views.login.util

import android.content.Context
import android.content.Intent
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.pda.uce.commom.util.*
import com.uce.pda.MyAppLication
import com.uce.pda.bean.User
import com.uce.pda.util.AssetsDBUtils
import com.pda.ym.callbacks.UICallBack

/**
 * @author: lj
 * Date: 2020/4/8 16:12
 * Description:
 */
class UcLoginUtil {
    /**
     * 登陆接口
     */
    open fun loginHttp(context: Context, userName: String, pwd: String,callback: UICallBack) {
        AssetsDBUtils.getPath()

        val json = JSONObject()
        json["systemCode"] = "PDA"
        if ("865959021414603" == AppUtil.getIMEI()) {
            json["loginTime"] = System.currentTimeMillis() + 360000
        } else {
            json["loginTime"] = System.currentTimeMillis()
        }
        json["userName"] = userName
        json["password"] = AppUtil.getMD5Str(pwd)
        json["serverId"] = "loginBiz"

//        if (isDingDingLoginModel && selectTrans != null) {
//            json["orgCode"] = selectTrans?.orgCode
//        }
        NetUtils.http_login(json) { t ->
            //根据不同的机型来，设置系统时间()
            val intent = Intent("com.android.update_settings_time")
            intent.putExtra("time", (t.getLongValue("systemTime")))
            context.sendBroadcast(intent)
            NewWorldMachineAdaptation.getInstance().setMachineSystemTime(t.getLongValue("systemTime"))
            AppUtil.setCurrentTime(t.getLongValue("systemTime"))
            if (t.getBoolean("success")) {
                ToastUtil.showToast("登录成功")
                val usr = JSON.parseObject(t.getJSONObject("result").toJSONString(), User::class.java)
                usr.password = pwd
                DBUtil.getDbUtil().save(usr)
                successLogin(t.getJSONObject("result"), userName, pwd)
                callback.onSuccess(null)
            } else {
                ToastUtil.showToast(t.getString("message"))
            }
        }
    }

    fun successLogin(t: JSONObject, userName: String, pwd: String) {
        Sp_net_Util.getInstance().put("userName", userName)
        Sp_net_Util.getInstance().put("password", pwd)
        Sp_net_Util.getInstance().put("orgCode", t.getString("orgCode"))
        Sp_net_Util.getInstance().put("orgName", t.getString("orgName"))
//        if (!TextUtils.equals(t.getString("empCode"), Sp_net_Util.getInstance().get("empCode"))) {
//            DBUtil.getDbUtil().delete(WhereBuilder.create(menu::class.java).where(" empCode = ?", arrayOf(Sp_net_Util.getInstance().get("empCode"))))
//        }
        AppUtil.saveJsontoSp(t)

        MyAppLication.deviceDataMap["empCode"] = SpUtil.getInstance().get("empCode")
        MyAppLication.deviceDataMap["taskDepartmentcode"] = Sp_net_Util.getInstance().get("orgCode")
        MyAppLication.deviceDataMap["orgCode"] = Sp_net_Util.getInstance().get("orgCode")
        MyAppLication.deviceDataMap["taskDepartment"] = Sp_net_Util.getInstance().get("orgName")
        MyAppLication.deviceDataMap["isExpressScan"] = SpUtil.getInstance().get("isExpressScan")

        // wym 可能出现的获取orgCode为null问题的规避
        if (MyAppLication.deviceDataMap["orgCode"] == null ||
                MyAppLication.deviceDataMap["empCode"] == null ||
                MyAppLication.deviceDataMap["taskDepartmentcode"] == null ||
                MyAppLication.deviceDataMap["taskDepartment"] == null) {
            ToastUtil.showToast("初始化数据出现问题")
        }

//        if (isDingDingLoginModel) {
////            Log.i("wym", "是否钉钉账号登录isDingDingLoginModel: $isDingDingLoginModel")
//            Sp_net_Util.getInstance().putBoolean("isDingDingLoginModel", true)
//        }
    }
}